package test;

import java.util.*;

public class Main {

    private static final String[] NORMAL_POKERS = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "JOKER"};

    /**
     * 翻译扑克 并按照从大到小的顺序排列
     *
     * @param poker
     * @return
     */
    public List<String> translate(List<String> poker) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < poker.size(); i++) {
            String s = poker.get(i);
            if (!s.toUpperCase().equals("JOKER")) {
                s = s.toUpperCase();
            }
            switch (s) {
                case "J":
                    list.add("11");
                    break;
                case "Q":
                    list.add("12");
                    break;
                case "K":
                    list.add("13");
                    break;
                case "A":
                    list.add("14");
                    break;
                case "2":
                    list.add("15");
                    break;
                case "joker":
                    list.add("16");
                    break;
                case "JOKER":
                    list.add("17");
                    break;
                default:
                    list.add(s);
                    break;
            }
        }
        return list;
    }

    /**
     * 按从大到小的顺序排列扑克
     *
     * @param poker
     * @return
     */
    public static List<String> sortListDESC(List<String> poker) {
        poker.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (Integer.parseInt(o1) > Integer.parseInt(o2)) {
                    return -1;
                } else if (Integer.parseInt(o1) < Integer.parseInt(o2)) {
                    return 1;
                }
                return 0;
            }
        });
        return poker;
    }

    /**
     * 比较两个数的大小
     *
     * @param a
     * @param b
     * @return 1 --- a>b  -1--- a<b 0 --- a===b
     */
    public int compare(String a, String b) {
        if (Integer.parseInt(a) > Integer.parseInt(b)) {
            return 1;
        } else if (Integer.parseInt(a) < Integer.parseInt(b)) {
            return -1;
        }
        return 0;
    }

    /**
     * list转成string
     *
     * @param list
     * @return
     */
    public String listToString(List<String> list) {
        StringBuilder sb = new StringBuilder();
        for (String s : list) {
            sb.append(s);
        }
        return sb.toString();
    }

    /**
     * 判断一手牌中的牌是否都一样 从而判断是对子 三张 还是炸弹
     *
     * @param poker
     * @return
     */
    public boolean isAllSame(List<String> poker) {
        if (poker == null || poker.size() == 0) {
            return false;
        }
        String firstPoker = poker.get(0);
        for (String s : poker) {
            if (!s.equals(firstPoker)) {
                return false;
            }
        }
        return true;
    }

    public boolean isShunzi(List<String> poker) {
        if (poker == null || poker.size() != 5) {
            return false;
        }

        for (int i = 0; i < poker.size() - 1; i++) {
            if (Integer.parseInt(poker.get(i)) - 1 != Integer.parseInt(poker.get(i + 1))) {
                return false;
            }
        }

        return true;
    }

    /**
     * 判断是否是大小王
     *
     * @param poker
     * @return
     */
    public boolean isJokers(List<String> poker) {
        if (poker != null && poker.size() == 2) {
            if (poker.get(0).toUpperCase().equals("JOKER") && poker.get(1).toUpperCase().equals("JOKER")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 比较两手牌的大小
     *
     * @param pokerA
     * @param pokerB
     * @return
     */
    public int compare(List<String> pokerA, List<String> pokerB) {

        // 根据数量先判断
        int numA = pokerA.size();
        int numB = pokerB.size();

        // 判断是否是大小王
        if (isJokers(pokerA)) {
            if (isJokers(pokerB)) {
                return 0;
            } else {
                return 1;
            }
        }

        if (isJokers(pokerB)) {
            return -1;
        }

        // 没有大小王的时候
        List<String> tempPokerA = translate(pokerA);
        List<String> tempPokerB = translate(pokerB);

        tempPokerA = sortListDESC(tempPokerA);
        tempPokerB = sortListDESC(tempPokerB);

        String pA = listToString(tempPokerA);
        String pB = listToString(tempPokerB);

       if (numA == 5 && numB == 5) {
            // 顺子
            if (isShunzi(tempPokerA) && isShunzi(tempPokerB)) {
                return compare(tempPokerA.get(4), tempPokerB.get(4));
            }
            return -2;
        }else if(numA == 4 || numB == 4){
            // 炸弹
            if (isAllSame(pokerA)){
                //A是炸弹
                if (isAllSame(pokerB)){
                    //B也是炸弹
                    return compare(pA, pB);
                }else{
                    //B不是炸弹
                    return 1;
                }
            }

            if (isAllSame(pokerB)){
                //说明B是炸弹 A不是炸弹
                return -1;
            }
            // A不是炸弹 B也不是炸弹 无法比较
           return -2;
        } else {
            // 单张 对子 三张
           if (pokerA.size() != pokerB.size()){
               return -2;
           }
            if (isAllSame(pokerA) && isAllSame(pokerB)) {
                return compare(pA, pB);
            }
            return -2;
        }
    }
    /**
     * 打印结果
     *
     * @param poker
     */
    public void printPoker(List<String> poker) {
        StringBuilder res = new StringBuilder();
        for (String s : poker) {
            res.append(s).append(" ");
        }
        System.out.println(res.toString().trim());
    }

    /**
     * 验证输入是否合法
     *
     * @param input
     * @return
     */
    public boolean validateInput(String input) {
        if (!input.contains("-")) {
            return false;
        }

        List<String> pokers = Arrays.asList(NORMAL_POKERS);
        String[] ps = input.split("-");

        for (String p : ps) {
            String[] ss = p.split(" ");
            for (String s : ss) {
                if (!pokers.contains(s.toUpperCase())) {
                    return false;
                }
            }
        }

        return true;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //System.out.println("请输入两手牌，以 - 分隔两手牌:");
        while (sc.hasNext()) {
            String input = sc.nextLine();
            Main poker = new Main();
            if (poker.validateInput(input)) {
                String[] pokers = input.split("-");
                if (pokers.length == 2) {
                    List<String> pokerA = Arrays.asList(pokers[0].split(" "));
                    List<String> pokerB = Arrays.asList(pokers[1].split(" "));
                    int compare = poker.compare(pokerA, pokerB);
                    //System.out.println("比较的结果是："+compare);
                    switch (compare) {
                        case -2:
                            System.out.println("ERROR");
                            break;
                        case -1:
                            poker.printPoker(pokerB);
                            break;
                        case 0:
                            System.out.println("两手牌一样大");
                            break;
                        case 1:
                            poker.printPoker(pokerA);
                            break;
                    }
                }
            } else {
                System.out.println("输入不符合规范，请重新输入");
            }
        }

    }


}
